<font size=18><a href="~audio/Ch09/09fig003.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 9.3 - A portion of a <b>Shape</b> class hierarchy.<img src="graphics/ch09/fig09003.gif" ></font><br>
</page>
<page>
<font size=18><a href="~audio/Ch09/09fig002.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 9.2 - An inheritance hierarchy for university community members.<img src="graphics/ch09/fig09002.gif" ></font><br>
</page>
<page>
<font size=18><a href="~audio/Ch09/09fig006.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 9.6 - Summary of base-class member accessibility in a derived class. (Part 1
of 2) </font><br>
<img src="graphics/ch09/fig0906a.gif" ><br>
</page>
<page>
<font size=18>Figure 9.6 - Summary of base-class member accessibility in a derived class. (Part 2
a) If the class <b>Alpha</b> inherits from the class <b>Beta</b>, class <b>Alpha</b> is called the
________ class and class <b>Beta</b> is called the ________ class.<br>
b) C++ provides for ________ which allows a derived class to inherit from
many base classes, even if these base classes are unrelated.<br>
c) Inheritance enables ________ which saves time in development, and
encourages using previously proven and high-quality software.<br>
d) An object of a ________ class can be treated as an object of its
corresponding ________ class.<br>
e) To convert a base-class pointer to a derived class pointer, a ________ must
be used because the compiler considers this a dangerous operation.<br>
<foreign name="answers" url="^Answers::c:s0p0">
</page>
<page pagename="Exercise 9.1">
f) The three member access specifiers are ________, ________ and
________.<br>
g) When deriving a class from a base class with <b>public</b> inheritance, <b>public</b>
members of the base class become ________ members of the derived class,
and <b>protected</b> members of the base class become ________ members of the
derived class.<br>
h) When deriving a class from a base class with <b>protected</b> inheritance, <b>public</b>
members of the base class become ________ members of the derived class,
and <b>protected</b> members of the base class become ________ members of the
derived class.<br>
i) A "has a" relationship between classes represents ________ and an "is a"
relationship between classes represents _______ .<br>
<foreign name="answers" url="^Answers::c:s0p0">
</page>
<page pagename="Exercise 9.2">
<b>Exercise 9.2</b><br>
Consider the class <b>Bicycle</b>. Given your knowledge of some common
components of bicycles, show a class hierarchy in which the class <b>Bicycle</b>
inherits from other classes, which, in turn, inherit from yet other classes. Discuss
the instantiation of various objects of class <b>Bicycle</b>. Discuss inheritance from
class <b>Bicycle</b> for other closely related derived classes.<br>
<br>
</page>
<page pagename="Exercise 9.3">
<b>Exercise 9.3</b><br>
Briefly define each of the following terms: inheritance, multiple inheritance,
base class and derived class.<br>
<br>
<br>
</page>
<page pagename="Exercise 9.4">
<b>Exercise 9.4</b><br>
Discuss why converting a base-class pointer to a derived-class pointer is
considered dangerous by the compiler.<br>
<br>
<br>
</page>
<page pagename="Exercise 9.5">
<b>Exercise 9.5</b><br>
Distinguish between single inheritance and multiple inheritance.<br>
<br>
<br>
</page>
<page pagename="Exercise 9.6">
<b>Exercise 9.6</b><br>
(True/False) A derived class is often called a subclass because it represents a
subset of its base class, i.e., a derived class is generally smaller than its base
class.<br>
<br>
<br>
</page>
<page pagename="Exercise 9.7">
<b>Exercise 9.7</b><br>
(True/False) A derived-class object is also an object of that derived class's base
class.<br>
<br>
<br>
</page>
<page pagename="Exercise 9.8">
<b>Exercise 9.8</b><br>
Some programmers prefer not to use <b>protected</b> access because it breaks the
encapsulation of the base class. Discuss the relative merits of using <b>protected</b>
access vs. insisting on using <b>private</b> access in base classes.<br>
<br>
<br>
</page>
<page pagename="Exercise 9.9">
<b>Exercise 9.9</b><br>
Many programs written with inheritance could be solved with composition
instead, and vice versa. Discuss the relative merits of these approaches in the
context of the <b>Point</b>, <b>Circle</b>, <b>Cylinder</b> class hierarchy in this chapter. Rewrite
the program of <a href="^Code::c:s0p5"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.10</a> (and the supporting classes) to use composition rather
than inheritance. After you do this, reassess the relative merits of the two
approaches both for the <b>Point</b>, <b>Circle</b>, <b>Cylinder</b> problem and for object-oriented
programs in general.<br>
<br>
</page>
<page pagename="Exercise 9.10">
<b>Exercise 9.10</b><br>
Rewrite the <b>Point</b>, <b>Circle</b>, <b>Cylinder</b> program of <a href="^Code::c:s0p5"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.10</a> as a <b>Point</b>, <b>Square</b>,
<b>Cube</b> program. Do this two ways--once with inheritance and once with
composition.<br>
<foreign name="answers" url="^Answers::c:s0p3">
</page>
<page pagename="Exercise 9.11">
<b>Exercise 9.11</b><br>
In the chapter, we stated, "When a base-class member is inappropriate for a
derived class, that member can be overridden in the derived class with an
appropriate implementation." If this is done, does the derived-class-is-a-base-
class-object relationship still hold? Explain your answer.<br>
<br>
<br>
</page>
<page pagename="Exercise 9.12">
<b>Exercise 9.12</b><br>
Study the inheritance hierarchy of <a href="^Illustration::c:s0p3"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 9.2</a>. For each class, indicate some
common attributes and behaviors consistent with the hierarchy. Add some other
with <i>more</i> features than their base classes, the terms
superclass and subclass can be confusing; we will avoid
these terms. Because derived class objects may be
thought of as objects of their base classes, this implies
that more objects are associated with base classes and <br>
</page>
<page>
fewer objects are associated with derived classes, so it
is reasonable to call base classes "superclasses" and
derived classes "subclasses."<br>
<spacer width=16 height=1>Inheritance forms tree-like hierarchical structures. A
base class exists in a hierarchical relationship with its
derived classes. A class can certainly exist by itself, but
it is when a class is used with the mechanism of
inheritance that the class becomes either a base class
that supplies attributes and behaviors to other classes,
or the class becomes a derived class that inherits
attributes and behaviors.<br>
<spacer width=16 height=1>Let us develop a simple inheritance hierarchy. A typical
university community has thousands of people who are <br>
</page>
<page>
community members. These people consist of
employees, students and alumni. Employees are either
faculty members or staff members. Faculty members
are either administrators (such as deans and department
chairpersons) or teaching faculty. This yields the
inheritance hierarchy shown in <a href="^Illustration::c:s0p3"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 9.2</a>. Note that some
administrators also teach classes, so we have used
multiple inheritance to form class
<b>AdministratorTeacher</b>. Because students often work
for their universities, and because employees often take
courses, it would also be reasonable to use multiple
inheritance to create a class called <b>EmployeeStudent</b>.<br>
</page>
<page>
Another substantial inheritance hierarchy is the <b>Shape</b>
hierarchy of <a href="^Illustration::c:s0p2"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 9.3</a>. A common observation among
students learning object-oriented programming is that
there are abundant examples of hierarchies in the real
world. It is just that these students are not accustomed
to categorizing the real world in this manner, so it takes
some adjustment in their thinking.<br>
<spacer width=16 height=1>Let us consider the syntax for indicating inheritance. To
specify that class <b>CommissionWorker</b> is derived from
class <b>Employee</b>, class <b>CommissionWorker</b> would
typically be defined as follows<br>
</page>
<page>
<font size=2><br></font><font size=11><pre>
class CommissionWorker : public Employee {<p>
...<p>
};<p>
</pre></font>
This is called <b><i>public</i></b> <i>inheritance</i> and is the most
commonly used type of inheritance. We will also
discuss <b><i>private inheritance</i></b> and <b><i>protected inheritance</i></b>.
With <b>public</b> inheritance, the <b>public</b> and <b>protected</b>
members of the base class are inherited as <b>public</b> and
<b>protected</b> members of the derived class, respectively.
Remember that <b>private</b> members of a base class are not
accessible from that class's derived classes. Note that
<b>friend</b> functions are not inherited.<br>
</page>
<page>
It is possible to treat base-class objects and derived-
class objects similarly; that commonality is expressed
in the attributes and behaviors of the base class. Objects
of any class derived with <b>public</b> inheritance from a
common base class can all be treated as objects of that
base class. We will consider many examples in which
we can take advantage of this relationship with an ease
of programming not available in non-object-oriented
languages, such as C.<br>
</page>
<page>
<b>Select the true statement(s). </b><br>
<component type="checkbox" width=20 height=18 label="" name="" feedback="False. A derived class is sometimes referred to as a subclass.">
A derived class is sometimes referred to as a superclass, because it contains information from multiple classes. <br>
<component type="checkbox" width=20 height=18 label="" name="" feedback="False. A : is used to indicate inheritance. C++ does not provide a keyword inherits.">
The inherits keyword is used to indicate inheritance. <br>
Derived class functions can directly access base class members designated public and protected. <br>
<component type="checkbox" width=20 height=18 label="" name="" feedback="False. A friend member of any class can access that class's protected members.">
A friend member of the base class cannot access protected members. <br>
<component type=button name=b label="Check Your Answer" width=125 height=24>
</page>
</section>
<section type=Body name=Default title="9.4 Casting Base-Class Pointers to Derived-Class Pointers">
<page>
<font size=18 bold>9.4 Casting Base-Class Pointers to Derived-
Class Pointers</font><hr>
An object of a publicly derived class can also be treated
as an object of its corresponding base class. This makes
possible some interesting manipulations. For example,
despite the fact that objects of a variety of classes
derived from a particular base class may be quite
different from one another, we can still create a linked
list of them--again, as long as we treat them as base-
class objects. <a href="^Errors::c:s0p0"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>But the reverse is not true: A base-class
object is not also automatically a derived-class object. <br>
</page>
<page>
The programmer may, however, use an explicit cast to
convert a base-class pointer to a derived-class pointer.
<a href="^Errors::c:s0p1"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>But be careful--if such a pointer is to be dereferenced,
then the programmer should be sure that the type of the
pointer matches the type of the object to which it points.
Our treatment in this section uses techniques widely
available in most compilers. In Chapter 21, we revisit
many of these topics in the context of the latest
compilers that conform to recent features of the ANSI/
ISO C+ draft standard, such as run-time type
identification (RTTI), <b>dynamic_cast</b> and <b>typeid</b>.<br>
<spacer width=16 height=1>Our first example is shown in <a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.4</a>, parts 1 through
6. Parts 1 and 2 show the <b>Point</b> class definition and <br>
</page>
<page>
<b>Point</b> member function definitions. Parts 3 and 4 show
the <b>Circle</b> class definition and <b>Circle</b> member function
definitions. Part 5 shows a driver program in which we
demonstrate assigning derived-class pointers to base-
class pointers and casting base-class pointers to
derived-class pointers. Part 6 shows the program
output.<br>
<spacer width=16 height=1>Let us first examine the <b>Point</b> class definition (<a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.4</a>,
part 1). The <b>public</b> interface to <b>Point</b> includes member
functions <b>setPoint</b>, <b>getX</b> and <b>getY</b>. The data members <b>x</b>
and <b>y</b> of <b>Point</b> are specified as <b>protected</b>. This prevents
clients of <b>Point</b> objects from directly accessing the
data, but enables classes derived from <b>Point</b> to access <br>
</page>
<page>
the inherited data members directly. If the data were
<b>private</b>, the <b>public</b> member functions of <b>Point</b> would
need to be used to access the data, even by derived
classes. Note that the <b>Point</b> overloaded stream-insertion
operator function (<a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.4</a>, part 2) is able to reference
variables <b>x</b> and <b>y</b> directly because the overloaded
stream-insertion operator function is a <b>friend</b> of class
<b>Point</b>. Note also that it is necessary to reference <b>x</b> and <b>y</b>
through objects as in <b>p.x</b> and <b>p.y</b>. This is because the
overloaded stream-insertion operator function is not a
member function of the class <b>Point</b> so we must use an
explicit handle so the compiler knows what object we
are referencing. Note that this class offers inlined <br>
</page>
<page>
<b>public</b> member functions <b>getX</b> and <b>getY</b> so
<b>operator<<</b> does not need to be a <b>friend</b> to achieve
good performance. However, needed <b>public</b> member
functions may not be provided in the <b>public</b> interface of
every class, so friendship is often appropriate.<br>
<spacer width=16 height=1>Class <b>Circle</b> (<a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.4</a>, part 3) inherits from class <b>Point</b>
with <b>public</b> inheritance. This is specified in the first
line of the class definition<br>
<font size=2><br></font><font size=11><pre>
class Circle : public Point { <p>
// Circle inherits from Point<p>
</pre></font>
The colon (<b>:</b>) in the header of the class definition
indicates inheritance. The keyword <b>public</b> indicates the
type of inheritance. (In<a href="#s7p0"> Section 9.7</a> we will discuss <br>
</page>
<page>
<b>protected</b> and <b>private</b> inheritance.) All the <b>public</b> and
<b>protected</b> members of class <b>Point</b> are inherited as
<b>public</b> and <b>protected</b> members, respectively, into class
<b>Circle</b>. This means that the <b>public</b> interface to <b>Circle</b>
includes the <b>Point</b> <b>public</b> members as well as the
<b>Circle</b> <b>public</b> members <b>area</b>, <b>setRadius</b> and
<b>getRadius</b>. <br>
The <b>Circle</b> constructor (<a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.4</a>, part 4) must invoke
the <b>Point</b> constructor to initialize the <b>Point</b> base-class
portion of a <b>Circle</b> object. This is accomplished with a
member initializer (introduced in Chapter 7) as follows<br>
<font size=2><br></font><font size=11><pre>
Circle::Circle( double r, int a, int b )<p>
: Point( a, b ) // call base-class constructor<p>
</pre></font>
</page>
<page>
The second line of the constructor function header
invokes the <b>Point</b> constructor by name. Values <b>a</b> and <b>b</b>
are passed from the <b>Circle</b> constructor to the <b>Point</b>
constructor to initialize the base-class members <b>x</b> and <b>y</b>.
If the <b>Circle</b> constructor did not invoke the <b>Point</b>
constructor explicitly, the default <b>Point</b> constructor
would be invoked implicitly with the default values for
<b>x</b> and <b>y</b> (i.e., 0 and 0). If in this case the <b>Point</b> class did
not provide a default constructor, the compiler would
issue a syntax error. Note that the <b>Circle</b> overloaded
<b>operator<<</b> function is able to output the <b>Point</b> part of
the <b>Circle</b> by casting the <b>Circle</b> reference <b>c</b> to a <b>Point</b>.
This results in a call to <b>operator<<</b> for <b>Point</b> and <br>
</page>
<page>
outputs the <b>x</b> and <b>y</b> coordinates using the proper <b>Point</b>
formatting.<br>
<spacer width=16 height=1>The driver program (<a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.4</a>, part 5) creates <b>pointPtr</b>
as a pointer to a <b>Point</b> object and instantiates <b>Point</b>
object <b>p</b>, then creates <b>circlePtr</b> as a pointer to a <b>Circle</b>
object and instantiates <b>Circle</b> object <b>c</b>. The objects <b>p</b>
and <b>c</b> are output using their overloaded stream-insertion
operators to show that they were initialized correctly.
Next, the driver assigns a derived-class pointer (the
address of object <b>c</b>) to base-class pointer <b>pointPtr</b> and
outputs the <b>Circle</b> object <b>c</b> using <b>operator<<</b> for <b>Point</b>
and the dereferenced pointer <b>*pointPtr</b>. Note that only
the <b>Point</b> portion of the <b>Circle</b> object <b>c</b> is displayed. <br>
</page>
<page>
With <b>public</b> inheritance, it is always valid to assign a
derived-class pointer to a base-class pointer because a
derived-class object<i> is a</i> base-class object. The base-
class pointer "sees" only the base-class part of the
derived-class object. The compiler performs an implicit
conversion of the derived-class pointer to a base-class
pointer. <br>
<spacer width=16 height=1>Then, the driver program demonstrates assigning a
derived-class pointer (the address of object <b>c</b>) to base-
class pointer <b>pointPtr</b> and casting <b>pointPtr</b> back to a
<b>Circle *</b>. The result of the cast operation is assigned to
<b>circlePtr</b>. The <b>Circle</b> object <b>c</b> is output using the
overloaded stream-insertion operator for <b>Circle</b> and the <br>
</page>
<page>
dereferenced pointer <b>*circlePtr.</b> The area of <b>Circle</b>
object <b>c</b> is output via <b>circlePtr</b>. This results in a valid
area value because the pointers are always pointing to a
<b>Circle</b> object. <br>
<spacer width=16 height=1>A base-class pointer cannot be assigned directly to a
derived-class pointer because this is an inherently
A base class pointer cannot be assigned directly to a derived class pointer without an explicit cast operation. <br>
<component type=button name=b label="Check Your Answer" width=125 height=24>
</page>
</section>
<section type=Body name=Default title="9.5 Using Member Functions">
<page>
<font size=18 bold>9.5 Using Member Functions</font><hr>
A derived class's member functions may need to access
certain of its base class's data members and member
functions. <br>
<spacer width=16 height=1>This is a crucial aspect of software engineering in C++.
If a derived class could access the <b><a href="^Engineer::c:s0p1"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a></b>base class's <b>private</b>
members, this would violate the encapsulation of the
base class. Hiding <b>private</b> members is a huge help in
testing, debugging and correctly modifying systems. If
a derived class could access its base class's <b>private</b>
members, it would then be possible for classes derived
from that derived class to access that data as well, and <br>
</page>
<page>
so on. This would propagate access to what is supposed
to be <b>private</b> data, and the benefits of encapsulation
would be lost throughout the class hierarchy.<br>
</page>
</section>
<section type=Body name=Default title="9.6 Overriding Base-Class Members in a Derived Class">
<page>
<font size=18 bold>9.6 Overriding Base-Class Members in a
Derived Class</font><hr>
A derived class can override a base-class member
function by supplying a new version of that function
with the same signature (if the signature were different,
this would be function overloading rather than function
overriding). When that function is mentioned by name
in the derived class, the derived-class version is
may be used to access the base-class version from the
derived class.<br>
</page>
<page>
Consider a simplified class <b>Employee</b>. It stores the
employee's <b>firstName</b> and <b>lastName</b>. This information
is common to all employees including those in classes
derived from class <b>Employee</b>. From class <b>Employee</b>
now derive classes <b>HourlyWorker</b>, <b>PieceWorker</b>,
<b>Boss</b> and <b>CommissionWorker</b>. The <b>HourlyWorker</b>
gets paid by the hour with "time-and-a-half" for
overtime hours in excess of 40 hours per week. The
<b>PieceWorker</b> gets paid a fixed rate per item
produced--for simplicity, assume this person makes
only one type of item, so the <b>private</b> data members are
number of items produced and rate per item. The <b>Boss</b>
gets a fixed wage per week. The <b>CommissionWorker</b> <br>
</page>
<page>
gets a small fixed weekly base salary plus a fixed
percentage of that person's gross sales for the week. For
simplicity, we study only class <b>Employee</b> and derived
class <b>HourlyWorker</b>.<br>
<spacer width=16 height=1>Our next example is shown in <a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.5</a>, parts 1 through
5. Parts 1 and 2 show the <b>Employee</b> class definition and
<b>Employee</b> member function definitions. Parts 3 and 4
show the <b>HourlyWorker</b> class definition and
<b>HourlyWorker</b> member function definition. Part 5
shows a driver program for the <b>Employee</b>/
<b>HourlyWorker</b> inheritance hierarchy that simply
instantiates an <b>HourlyWorker</b> object, initializes it and <br>
</page>
<page>
calls <b>HourlyWorker</b> member function <b>print</b> to output
the object's data. <br>
<spacer width=16 height=1>The <b>Employee</b> class definition (<a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.5</a>, part 1)
consists of two private <b>char *</b> data members--
<b>firstName</b> and <b>lastName</b>--and three member
functions--a constructor, a destructor and <b>print</b>. The
constructor function (<a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.5</a>, part 2) receives two
strings and dynamically allocates character arrays to
store the strings. Note that the <b>assert</b> macro (discussed
in Chapter 18, "Other Topics") is used to determine if
memory was allocated to <b>firstName</b> and <b>lastName</b>. If
not, the program terminates with an error message
indicating the condition tested, the line number on <br>
</page>
<page>
which the condition appears and the file in which the
condition is located. [Note, once again, that in the C++
draft standard, <b>new</b> "throws" an exception if
insufficient memory is available; we discuss this in
Chapter 13.] Because the data of <b>Employee</b> are
<b>private</b>, the only access to the data is through member
function <b>print</b> which simply outputs the first name and
last name of the employee. The destructor function
returns the dynamically allocated memory to the system
(to avoid a "memory leak").<br>
<spacer width=16 height=1>Class <b>HourlyWorker</b> (<a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.5</a>, part 3) inherits from
class <b>Employee</b> with <b>public</b> inheritance. Again, this is <br>
</page>
<page>
specified in the first line of the class definition using the
colon (<tt><b>:</b></tt>) notation as follows<br>
<font size=2><br></font><font size=11><pre>
class HourlyWorker : public Employee<p>
</pre></font>
The <b>public</b> interface to <b>HourlyWorker</b> includes the
<b>Employee</b> <b>print</b> function and <b>HourlyWorker</b> member
functions <b>getPay</b> and <b>print</b>. Note that class
<b>HourlyWorker</b> defines its own <b>print</b> function with the
same prototype as <b>Employee::print()</b>--this is an
example of function overriding. Therefore, class
<b>HourlyWorker</b> has access to two <b>print</b> functions.
Class <b>HourlyWorker</b> also contains <b>private</b> data <br>
</page>
<page>
members <b>wage</b> and <b>hours</b> for calculating the
An indirect base class is not explicitly listed in the derived class's definition, but is inherited in a class two or more levels up the class hierarchy. <br>
<component type=button name=b label="Check Your Answer" width=125 height=24>
</page>
</section>
<section type=Body name=Default title="9.9 Using Constructors and Destructors in Derived Classes">
<page>
<font size=18 bold>9.9 Using Constructors and Destructors in
Derived Classes</font><hr>
Because a derived class inherits its base class's
members, when an object of a derived class is
instantiated, the base class's constructor must be called
to initialize the base-class members of the derived-class
object. <a href="^Engineer::c:s0p4"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>A <i>base-class initializer</i> (which uses the
member-initializer syntax we have seen) can be
provided in the derived-class constructor to call the
<a href="^Engineer::c:s0p2"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>base-class constructor explicitly; otherwise, the derived
class's constructor will call the base class's default
constructor implicitly.<br>
</page>
<page>
Base-class constructors and base-class assignment
operators are not inherited by derived classes. Derived-
class constructors and assignment operators, however,
constructor for its base class first to initialize the
derived class's base-class members. If the derived-class
constructor is omitted, the derived class's default
constructor calls the base-class's default constructor.
Destructors are called in the reverse order of
constructor calls, so a derived-class destructor is called
before its base-class destructor.<br>
</page>
<page>
The program of <a href="^Code::c:s0p2"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.7</a> demonstrates the order in
which base-class and derived-class constructors and
destructors are called. The program consists of five
parts. Parts 1 and 2 show a simple <b>Point</b> class
containing a constructor, a destructor and <b>protected</b>
data members <b>x</b> and <b>y</b>. The constructor and destructor
both print the <b>Point</b> object for which they are invoked. <br>
<spacer width=16 height=1>Parts 3 and 4 of <a href="^Code::c:s0p2"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.7</a> show a simple <b>Circle</b> class
derived from <b>Point</b> with <b>public</b> inheritance. Class
<b>Circle</b> provides a constructor, a destructor and a
<b>private</b> data member <b>radius</b>. The constructor and
destructor both print the <b>Circle</b> object for which they
are invoked. The <b>Circle</b> constructor also invokes the <br>
</page>
<page>
<b>Point</b> constructor using member initializer syntax and
passes the values a and b so the base-class data
members <b>x</b> and <b>y</b> can be initialized.<br>
<spacer width=16 height=1>Part 5 of<a href="^Code::c:s0p2"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.7</a> is a driver program for this <b>Point</b>/
<b>Circle</b> hierarchy. The program begins by instantiating a
<b>Point</b> object in its own scope inside <b>main</b>. The object
goes in and out of scope immediately, so the <b>Point</b>
constructor and destructor are both called. Next, the
program instantiates <b>Circle</b> object <b>circle1</b>. This invokes
the <b>Point</b> constructor to perform output with values
passed from the <b>Circle</b> constructor, then performs the
output specified in the <b>Circle</b> constructor. <b>Circle</b> object
<b>circle2</b> is instantiated next. Again, the <b>Point</b> and <b>Circle</b> <br>
</page>
<page>
constructors are both called. Note that the body of the
<b>Point</b> constructor is performed before the body of the
<b>Circle</b> constructor. The end of <b>main</b> is reached, so the
destructors are called for objects <b>circle1</b> and <b>circle2</b>.
Destructors are called in the reverse order of their
corresponding constructors. Therefore, the <b>Circle</b>
destructor and <b>Point</b> destructor are called in that order
for object <b>circle2</b>, then the <b>Circle</b> and <b>Point</b> destructors
are called in that order for object <b>circle1</b>.<br>
</page>
<page>
<b>Select the true statement(s). </b><br>
<component type="checkbox" width=20 height=18 label="" name="" feedback="False. Constructors are not inherited.">
A base class pointer can safely refer to a derived class object's members that were inherited from the base class. <br>
<component type="checkbox" width=20 height=18 label="" name="" feedback="False. A derived class pointer must be explicitly cast if it is to point to an object of the base class.">
A derived class pointer can point to an object of the base class without the need for casting. <br>
<component type=button name=b label="Check Your Answer" width=125 height=24>
</page>
</section>
<section type=Body name=Default title="9.11 Software Engineering with Inheritance">
<page>
<font size=18 bold>9.11 Software Engineering with Inheritance</font><hr>
We can use inheritance to customize existing software.
We inherit the attributes and behaviors of an existing
class, then add attributes and behaviors (or override
base-class behaviors) to customize the class to meet our
needs. This is done in C++ without the derived class
having access to the base class's source code, but the
derived class does need to be able to link to the base
class's object code. <a href="^Engineer::c:s0p9"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>This powerful capability is
attractive to independent software vendors (ISVs). The
ISVs can develop proprietary classes for sale or license
and make these classes available to users in object-code <br>
</page>
<page>
format. Users can then derive new classes from these
library classes rapidly and without accessing the ISVs'
proprietary <a href="^Perform::c:s0p0"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>source code. All the ISVs need to supply
with the object code are the header files. <br>
<spacer width=16 height=1>It can be difficult for students to appreciate the
problems faced by designers and implementors on
large-scale software projects. People experienced on
such projects will invariably state that a key to
improving the software development process is
software reuse. Object-oriented programming in
general, and C++ in particular, certainly do this.<br>
<spacer width=16 height=1>It is the availability of substantial and useful class
libraries that delivers the maximum benefits of software <br>
</page>
<page>
reuse through inheritance. As interest in C++ grows,
interest in class libraries is growing exponentially. Just
as shrink-wrapped software produced by independent
software vendors became an explosive growth industry
with the arrival of the personal computer, so, too, is the
creation and sale of class libraries. Application
designers are building their applications with these
libraries, and library designers are being rewarded by
having their libraries wrapped with the applications.
Libraries currently being shipped with C++ compilers
tend to be rather general-purpose and limited in scope.
What is coming is a massive worldwide commitment to <br>
</page>
<page>
the development of class libraries for a huge variety of
applications arenas.<br>
<spacer width=16 height=1> <a href="^Engineer::c:s0p12"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>A base class specifies commonality--all classes
derived from a base class inherit the capabilities of that
base class. In the object-oriented design process, the
designer looks for<a href="^Engineer::c:s0p11"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a> commonality and "factors it out" to
form desirable base classes. Derived classes are then
customized beyond the capabilities inherited from the
base class.<br>
<spacer width=16 height=1>Just as the designer of non-object-oriented systems
seeks to avoid unnecessary proliferation of functions,
the designer of object-oriented systems should avoid
unnecessary proliferation of classes. Such a <br>
</page>
<page>
proliferation of classes creates management problems
and can hinder software reusability simply because it is
more difficult for a potential reuser of a class to locate
that class in a huge collection. <a href="^Perform::c:s0p1"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>The trade-off is to create
fewer classes, each providing substantial additional
functionality. Such classes might be too rich for certain
reusers; they can mask the excessive functionality, thus
"toning down" the classes to meet their needs. <br>
<spacer width=16 height=1> <a href="^Engineer::c:s0p14"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>Note that reading a set of derived-class declarations
can be confusing because inherited members are not
shown, <a href="^Engineer::c:s0p13"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>but they are nevertheless present in the derived
classes. A similar problem can exist in the
documentation of derived classes.<br>
</page>
</section>
<section type=Body name=Default title="9.12 Composition Vs. Inheritance">
<page>
<font size=18 bold>9.12 Composition Vs. Inheritance</font><hr>
We have discussed <i>is a</i> relationships which are
supported by <b>public</b> inheritance. We have also
discussed <i>has a</i> relationships (and seen examples in
preceding chapters) in which a class may have other
classes as members--<a href="^Engineer::c:s0p16"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>such relationships create new
classes by <i>composition</i> of existing classes. For example,
given the classes <b>Employee</b>, <b>BirthDate</b> and
<b>TelephoneNumber</b>, it is improper to say that an
<b>Employee</b> <i>is a</i> <b>BirthDate</b> or that an <b>Employee</b> <i>is a</i>
<b>TelephoneNumber</b>. But it is certainly appropriate to <br>
</page>
<page>
say that an <b>Employee</b> <i>has a</i> <b>BirthDate</b> and that an
<spacer width=16 height=1><a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 9.8</a> shows class <b>Point</b>. Part 1 is the class <b>Point</b>
definition. Note that Point's data members are
<b>protected</b>. Thus, when class <b>Circle</b> is derived from
class <b>Point</b>, the member functions of class <b>Circle</b> will <br>
</page>
<page>
be able to directly reference coordinates <b>x</b> and <b>y</b> rather
than using access functions. This may result in better
performance.<br>
<spacer width=16 height=1><a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 9.8</a>, part 2, shows the member function
definitions for class <b>Point</b>.<a href="^Code::c:s0p3"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Figure 9.8</a>, part 3, shows a
driver program for class <b>Point</b>. Note that <b>main</b> must
use the access functions <b>getX</b> and <b>getY</b> to read the
values of <b>protected</b> data members <b>x</b> and <b>y</b>; remember
that <b>protected</b> data members are accessible only to
members and <tt><b>friend</b></tt>s of their class and members and
<b>friend</b>s of their derived classes.<br>
<spacer width=16 height=1>Our next example is shown in <a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.9</a>, parts 1 through
5. The <b>Point</b> class definition and the member function <br>
</page>
<page>
definitions from <a href="^Code::c:s0p3"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.8</a> are reused here. Parts 1
through 5 show the <b>Circle</b> class definition, <b>Circle</b>
member function definitions and a driver program with
output. Note that class <b>Circle</b> inherits from class <b>Point</b>
with <b>public</b> inheritance. This means that the <b>public</b>
interface to <b>Circle</b> includes the <b>Point</b> member functions
as well as the <b>Circle</b> member functions <b>setRadius</b>,
<b>getRadius</b> and <b>area</b>. <br>
<spacer width=16 height=1>Note that the <b>Circle</b> overloaded <tt><b>operator<<</b></tt> function
which is a <b>friend</b> of class <b>Circle</b> is able to output the
<b>Point</b> part of the <b>Circle</b> by casting the <b>Circle</b> reference
<b>c </b>to a <b>Point</b>. This results in a call to <b>operator<<</b> for <br>
</page>
<page>
<b>Point</b> and outputs the <b>x</b> and <b>y</b> coordinates using the
proper <b>Point</b> formatting. <br>
The driver program in part 4 of <a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.9</a> instantiates an
object of class <b>Circle</b> then uses <i>get</i> functions to obtain
the information about the <b>Circle</b> object. Again, <b>main</b> is
neither a member function nor a <b>friend</b> of class <b>Circle</b>
so it cannot directly reference the <b>protected</b> data of
class <b>Circle</b>. The driver program then uses <i>set</i> functions
<b>setRadius</b> and <b>setPoint</b> to reset the radius and
coordinates of the center of the circle. Finally, the driver
initializes reference variable <b>pRef</b> of type "reference to
<b>Point</b> object" (<b>Point &</b>) to <b>Circle</b> object <b>c</b>. The driver
then prints <b>pRef</b> which, despite the fact that it is <br>
</page>
<page>
initialized with a <b>Circle</b> object, "thinks" it is a <b>Point</b>
object, so the <b>Circle</b> object actually prints as a <b>Point</b>
object.<br>
<spacer width=16 height=1>Our last example is shown in<a href="^Code::c:s0p5"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.10</a>, parts 1 through
5. The <b>Point</b> class and <b>Circle</b> class definitions, and
their member function definitions from<a href="^Code::c:s0p3"> <img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.8</a> and
<a href="^Code::c:s0p4"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.9</a> are reused here. Parts 1 through 5 show the
<b>Cylinder</b> class definition (part 1), <b>Cylinder</b> member
function definitions (parts 2 and 3) and a driver
program with output (parts 4 and 5). Note that class
<b>Cylinder</b> inherits from class <b>Circle</b> with <b>public</b>
inheritance. This means that the <b>public</b> interface to
<b>Cylinder</b> includes the <b>Circle</b> member functions and <br>
</page>
<page>
<b>Point</b> member functions as well as the <b>Cylinder</b>
member functions <b>setHeight</b>, <b>getHeight</b>, <b>area</b>
(overridden from <b>Circle</b>) and <b>volume</b>. Note that the
<b>Cylinder</b> constructor is required to invoke the
constructor for its direct base class <b>Circle</b>, but not for
its indirect base class <b>Point</b>. Each derived class
constructor is only responsible for calling the
constructors of that class's immediate base class (or
classes, in the case of multiple inheritance). Also, note
that the <b>Cylinder</b> overloaded <b>operator<<</b> function
which is a <b>friend</b> of class <b>Cylinder</b> is able to output the
<b>Circle</b> part of the <b>Cylinder</b> by casting the <b>Cylinder</b>
reference <b>c</b> to a <b>Circle</b>. This results in a call to <br>
</page>
<page>
<b>operator<<</b> for <b>Circle</b> and outputs the <b>x</b> and <b>y</b>
coordinates and the <b>radius</b> using the proper <b>Circle</b>
formatting.<br>
<spacer width=16 height=1>The driver program instantiates an object of class
<b>Cylinder</b> then uses <i>get</i> functions to obtain the
information about the <b>Cylinder</b> object. Again, <b>main</b> is
neither a member function nor a <b>friend</b> of class
<b>Cylinder</b> so it cannot directly reference the <b>protected</b>
data of class <b>Cylinder</b>. The driver program then uses
<i>set</i> functions <b>setHeight</b>, <b>setRadius</b> and <b>setPoint</b> to
reset the height, radius and coordinates of the cylinder.
Finally, the driver initializes reference variable <b>pRef</b> of
type "reference to <b>Point</b> object" (<b>Point &</b>) to <b>Cylinder</b> <br>
</page>
<page>
object <b>cyl</b>. It then prints <b>pRef</b> which, despite the fact
that it is initialized with a <b>Cylinder</b> object, "thinks" it is
a <b>Point</b> object, so the <b>Cylinder</b> object actually prints as
a <b>Point</b> object. The driver then initializes reference
variable <b>circleRef</b> of type "reference to <b>Circle</b> object"
(<b>Circle &</b>) to <b>Cylinder</b> object <b>cyl</b>. The driver program
then prints <b>circleRef</b> which, despite the fact that it is
initialized with a <b>Cylinder</b> object, "thinks" it is a
<b>Circle</b> object, so the <b>Cylinder</b> object actually prints as
a <b>Circle</b> object. The area of the <b>Circle</b> is also output.<br>
<spacer width=16 height=1>This example nicely demonstrates <b>public</b> inheritance
and defining and referencing <b>protected</b> data members.
The reader should now be confident with the basics of <br>
</page>
<page>
inheritance. In the next chapter, we show how to
program with inheritance hierarchies in a general
manner using polymorphism. Data abstraction,
inheritance and polymorphism are the crux of object-
inheritance in which each class is derived from exactly
one base class. A class may be derived from more than
one base class; such derivation is called <i>multiple
inheritance</i>. <a href="^Practice::c:s0p0"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>Multiple inheritance means that a derived
class inherits the members of several base classes. This
powerful capability encourages interesting forms of
software reuse, but can cause a variety of ambiguity
problems.<br>
<spacer width=16 height=1>Consider the multiple inheritance example in <a href="^Code::c:s0p6"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 9.11</a>.
Class <b>Base1</b> contains one <b>protected</b> data member--<b>int</b> <br>
</page>
<page>
<b>value</b>. <b>Base1</b> contains a constructor that sets <b>value</b> and
<b>public</b> member function <b>getData</b> that returns <b>value</b>.<br>
<spacer width=16 height=1>Class <b>Base2</b> is similar to class <b>Base1</b> except that its
<b>protected</b> data is <b>char letter</b>.<b> Base2</b> also has a <b>public</b>
member function <b>getData</b>, but this function returns the
value of <b>char letter</b>.<br>
<spacer width=16 height=1>Class <b>Derived</b> is inherited from both class <b>Base1</b> and
class <b>Base2</b> through multiple inheritance. <b>Derived</b> has
<b>private</b> data member <b>float</b> <b>real</b> and has <b>public</b> member
function <b>getReal</b> that reads the value of <b>float real</b>. <br>
<spacer width=16 height=1>Note how straightforward it is to indicate multiple
inheritance by following the colon (<b>:</b>)<b> </b> after <b>class</b>
<b>Derived</b> with a comma-separated list of base classes. <br>
</page>
<page>
Note also that constructor <b>Derived</b> explicitly calls base-
class constructors for each of its base classes, <b>Base1</b>
and <b>Base2</b>, through the member-initializer syntax.
Again, base-class constructors are called in the order
that the inheritance is specified, not in the order in
which their constructors are mentioned. And if the
base-class constructors are not explicitly called in the
member initializer list, their default constructors will be
called implicitly.<br>
The overloaded stream-insertion operator for <b>Derived</b>
uses dot notation off the derived object d to print <b>value</b>,
<b>letter</b> and <b>real</b>. This operator function is a <b>friend</b> of
<b>Derived</b>, so <b>operator<<</b> can directly access <b>private</b> <br>
</page>
<page>
data member <b>real</b> of <b>Derived</b>. Also, because this
operator is a <b>friend</b> of a derived class, it can access the
<b>protected</b> members <b>value</b> and <b>letter</b> of <b>Base1</b> and
<b>Base2</b>, respectively.<br>
<spacer width=16 height=1>Now let us examine the driver program in <b>main</b>. We
create object <b>b1</b> of class <b>Base1</b> and initialize it to <b>int</b>
value <b>10</b>. We create object <b>b2</b> of class <b>Base2</b> and
initialize it to <b>char</b> value '<b>Z</b>'. Then, we create object <b>d</b>
of class <b>Derived</b> and initialize it to contain <b>int</b> value <b>7</b>,
<b>char</b> value '<b>A</b>' and <b>float</b> value <b>3.5</b>.<br>
<spacer width=16 height=1>The contents of each of the base-class objects is printed
by calling the <b>getData</b> member function for each
object. Even though there are two <b>getData</b> functions, <br>
</page>
<page>
the calls are not ambiguous because they refer directly
to the object <b>b1</b> version of <b>getData</b> and the object <b>b2</b>
version of <b>getData</b>. <br>
<spacer width=16 height=1>Next we print the contents of <b>Derived</b> object <b>d</b> with
static binding. But we do have an ambiguity problem
because this object contains two <b>getData</b> functions, one
inherited from <b>Base1</b> and one inherited from <b>Base2</b>.
This problem is easy to solve by using the binary scope
resolution operator as in <b>d.Base1::getData()</b> to print
the <b>int</b> in <b>value</b> and <b>d.Base2::getData()</b> to print the
<b>char</b> in <b>letter</b>. The <b>float</b> value in <b>real</b> is printed without
ambiguity with the call <b>d.getReal()</b>. Next we
demonstrate that the <i>is a</i> relationships of single <br>
</page>
<page>
inheritance also apply to multiple inheritance. We
assign the address of derived object <b>d</b> to base-class
pointer <b>base1Ptr</b> and we print <b>int</b> <b>value</b> by invoking
<b>Base1</b> member function <b>getData</b> off <b>base1Ptr</b>. We then
assign the address of derived object <b>d</b> to base-class
pointer <b>base2Ptr</b> and we print <b>char</b> <b>letter</b> by invoking
<b>Base2</b> member function <b>getData</b> off <b>base2Ptr</b>.<br>
<spacer width=16 height=1>This example showed the mechanics of multiple
inheritance in a simple example and introduced a
simple ambiguity problem. <a href="^Engineer::c:s0p18"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>Multiple inheritance is a
complex topic dealt with in more detail in advanced
<indent width=8 delay>* One of the keys to the power of object-oriented programming is achieving software reusability through
inheritance. </indent>
<indent width=8 delay>* The programmer can designate that the new class is
to inherit the data members and member functions of a
previously defined base class. In this case, the new class
is referred to as a derived class. </indent>
<indent width=8 delay>* With single inheritance, a class is derived from only
one base class. With multiple inheritance, a derived
class inherits from multiple (possibly unrelated) base
classes. </indent>
</page>
<page>
<indent width=8 delay>* A derived class normally adds data members and
member functions of its own, so a derived class generally has a larger definition than its base class. A derived
class is more specific than its base class and normally
represents fewer objects.</indent>
<indent width=8 delay>* A derived class cannot access the <b>private</b> members
of its base class; allowing this would violate the encapsulation of the base class. A derived class can, however,
access the <b>public</b> and <b>protected</b> members of its base
class. </indent>
<indent width=8 delay>* A derived-class constructor always calls the constructor for its base class first to create and initialize the
derived class's base-class members.</indent>
</page>
<page>
<indent width=8 delay>* Destructors are called in the reverse order of constructor calls, so a derived-class destructor is called
before its base-class destructor. </indent>
<indent width=8 delay>* Inheritance enables software reusability which saves
time in development and encourages the use of previously proven and debugged high-quality software. </indent>
<indent width=8 delay>* Inheritance can be accomplished from existing class
libraries.</indent>
<indent width=8 delay>* Someday most software will be constructed from
standardized reusable components exactly as most
hardware is constructed today. </indent>
<indent width=8 delay>* The implementor of a derived class does not need
access to the source code of a base class, but does need </indent>
</page>
<page>
<indent width=8 delay>* the interface to the base class and the base class's object
code.</indent>
<indent width=8 delay>* An object of a derived class can be treated as an
object of its corresponding public base class. However,
the reverse is not true.</indent>
<indent width=8 delay>* A base class exists in a hierarchical relationship with
its singly derived classes. </indent>
<indent width=8 delay>* A class can exist by itself. When that class is used
with the mechanism of inheritance, it becomes either a
base class that supplies attributes and behaviors to other
classes, or the class becomes a derived class that inherits those attributes and behaviors.</indent>
<indent width=8 delay>* An inheritance hierarchy can be arbitrarily deep </indent>
</page>
<page>
<indent width=8 delay>* within the physical limitations of a particular system.</indent>
<indent width=8 delay>* Hierarchies are useful tools for understanding and
managing complexity. With software becoming increasingly complex, C++ provides mechanisms for supporting hierarchical structures through inheritance and
polymorphism.</indent>
<indent width=8 delay>* An explicit cast can be used to convert a base-class
pointer to a derived-class pointer. Such a pointer should
not be dereferenced unless it actually points to an object
of the derived class type.</indent>
<indent width=8 delay>* <b>Protected</b> access serves as an intermediate level of
protection between <b>public</b> access and <b>private</b> access.
<b>Protected</b> members of a base class may be accessed by </indent>
</page>
<page>
<indent width=8 delay>* members and<b> friend</b>s of the base class and by members
and <b>friend</b>s of derived classes; no other functions can
access the <b>protected</b> members of a base class. </indent>
<indent width=8 delay>* <b>Protected</b> members are used to extend privileges to
derived classes while denying those privileges to non-
class, non-<b>friend</b> functions.</indent>
<indent width=8 delay>* Multiple inheritance is indicated by placing a colon
(<b>:</b>) after the derived-class name and following the colon
with a comma-separated list of base classes. Member
initializer syntax is used in the derived-class constructor
to call base-class constructors.</indent>
<indent width=8 delay>* When deriving a class from a base class, the base
class may be declared as either <b>public</b>, <b>protected</b> or </indent>
</page>
<page>
<indent width=8 delay>* <b>private</b>.</indent>
<indent width=8 delay>* When deriving a class from a <b>public</b> base class, <b>public</b> members of the base class become public members of the derived class, and <b>protected</b> members of the
base class become <b>protected</b> members of the derived
class. </indent>
<indent width=8 delay>* When deriving a class from a <b>protected</b> base class,
<b>public</b> and <b>protected</b> members of the base class
become <b>protected</b> members of the derived class. </indent>
<indent width=8 delay>* When deriving a class from a <b>private</b> base class,
<b>public</b> and <b>protected</b> members of the base class
become <b>private</b> members of the derived class.</indent>
<indent width=8 delay>* A base class may be either a direct base class of a </indent>
</page>
<page>
<indent width=8 delay>* derived class or an indirect base class of a derived class.
A direct base class is explicitly listed where the derived
class is declared. An indirect base class is not explicitly
listed; rather it is inherited from several levels up the
class hierarchy tree.</indent>
<indent width=8 delay>* When a base-class member is inappropriate for a
derived class, we may simply redefine that member in
the derived class.</indent>
<indent width=8 delay>* It is important to distinguish between "is a" relationships and "has a" relationships. In a "has a" relationship, a class object has an object of another class as a
member. In an "is a" relationship, an object of a
derived-class type may also be treated as an object of </indent>
</page>
<page>
<indent width=8 delay>* the base-class type. "Is a" is inheritance. "Has a" is
composition.</indent>
<indent width=8 delay>* A derived class object can be assigned to a base class
object. This kind of assignment makes sense because
the derived class has members corresponding to each of
the base class members.</indent>
<indent width=8 delay>* A pointer to a derived-class object may be implicitly
converted into a pointer for a base-class object.</indent>
<indent width=8 delay>* It is possible to convert a base-class pointer to a
derived-class pointer by using an explicit cast. The target should be a derived class object.</indent>
<indent width=8 delay>* A base class specifies commonality. All classes
derived from a base class inherit the capabilities of that </indent>
</page>
<page>
<indent width=8 delay>* base class. In the object-oriented design process, the
designer looks for commonality and factors it out to
form desirable base classes. Derived classes are then
customized beyond the capabilities inherited from the
base class.</indent>
<indent width=8 delay>* Reading a set of derived-class declarations can be
confusing because not all the members of the derived
class are present in these declarations. In particular,
inherited members are not listed in the derived-class
declarations, but these members are indeed present in
the derived classes.</indent>
<indent width=8 delay>* "Has a" relationships are examples of creating new
classes by composition of existing classes. </indent>
</page>
<page>
<indent width=8 delay>* "Knows a" relationships are examples of objects containing pointers or references to other objects so they
can be aware of those objects. </indent>
<indent width=8 delay>* Member object constructors are called in the order in
which the objects are declared. In inheritance, base-
class constructors are called in the order in which inheritance is specified and before the derived-class constructor.</indent>
<indent width=8 delay>* For a derived-class object, first the base-class constructor is called, then the derived-class constructor is
called (which may call member object constructors). </indent>
<indent width=8 delay>* When the derived-class object is destroyed, the
destructors are called in the reverse order of the con</indent>
</page>
<page>
<indent width=8 delay>* structors--first the derived-class destructor is called,
then the base-class destructor is called. </indent>
<indent width=8 delay>* A class may be derived from more than one base
class; such derivation is called multiple inheritance. </indent>
<indent width=8 delay>* Indicate multiple inheritance by following the colon
(<b>:</b>) inheritance indicator with a comma-separated list of
base classes. </indent>
<indent width=8 delay>* The derived-class constructor calls base-class constructors for each of its base classes through the member-initializer syntax. Base-class constructors are called
in the order in which the base classes are declared during inheritance.</indent>
</page>
</section>
<section type=Popup name=Debug title="Testing">
<page>
This chapter does not contain any Testing and Debugging tips.